package com.bigknow.appstore.web.base.controller;

import com.bigknow.appstore.common.util.StringUtils;
import com.bigknow.appstore.web.base.entity.AuditLog;
import com.bigknow.appstore.web.base.service.IAuditLogService;
import com.bigknow.appstore.web.framework.controller.AbstractRestController;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping(value = "/base/auditLog")
public class AuditLogController extends AbstractRestController<AuditLog, IAuditLogService> {
    @Override
    protected String getBasePath() {
        return "/base";
    }

    @Override
    protected String getBaseName() {
        return "auditLog";
    }

    @RequestMapping(value = "/export/{type}/{dateRange}")
    public ModelAndView exportXls(@PathVariable String type, @PathVariable String dateRange, ModelMap map) {
        List<AuditLog> entities;
        if (StringUtils.isNoneBlank(dateRange) && dateRange.split("_").length == 2) {
            String[] temp = dateRange.split("_");
            entities = this.service.findByDateRange(temp[0].replace("T", " "), temp[1].replace("T", " "));
        } else {
            entities = this.service.findAll();
        }

        map.put(NormalExcelConstants.FILE_NAME, getExportFileName());
        map.put(NormalExcelConstants.CLASS, AuditLog.class);
        map.put(NormalExcelConstants.PARAMS, new ExportParams("应用商店", "导出信息"));
        map.put(NormalExcelConstants.DATA_LIST, entities);
        return new ModelAndView(NormalExcelConstants.JEECG_EXCEL_VIEW);
    }
}
